<?php
	class Staff extends CI_Controller {
	   
       function __construct() {
	       parent::__construct();
           $user = $this->session->userdata('user');
           if (!$user) redirect('main/alogin');
           $this->load->model('staff_db','Staff');
           $this->load->model('Dataset_db');
	   }
       
       function addstafftype() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddStaffType']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"type_label"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('type_label', 'Staff Type', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                
                if(!$this->Staff->is_stafftype_unique($this->input->post("type_label",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Staff Type already exist.");
                    else
                        $data["errors"] = array("Staff Type already exist.");
                }
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    //print_object($pdata); exit;
                    
                    $save_result = $this->Staff->save_stafftype($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Staff Type added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $this->load->view('addstafftype',$data);
        }
        
       function editstafftype() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Edit Staff Type";
            if( isset( $_POST["btnUpdateMenuGroup"] ) ) {
                //print_object($_POST);exit;
                $this->form_validation->set_rules('mglabel', 'Label', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('mgurl', 'Url', 'trim|required|min_length[3]|max_length[12]|xss_clean');
                $this->form_validation->set_rules('mgposition', 'Position', 'trim|required|numeric|xss_clean');
                $this->form_validation->set_rules('mgid', 'Menu Group ID', 'trim|required|numeric|xss_clean');
                if ($this->form_validation->run() == FALSE) {
                    $data["errors"] = $this->form_validation->error_array();
                } else {
                    $pdata = array();
                    $fields = array(
    						"mglabel"
    						,"mgurl"
    						,"mgposition"				
    						);
                    foreach ($fields as $field) {
        				if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field,true);
        			}
                    
                    //print_object($this->input->post('mgid',true));print_object($pdata); exit;
                    $save_result = $this->Menu->save_mg($pdata,$this->input->post('mgid',true));
                    //print_object($save_result); exit;
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Menu group updated successfully.');
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            } 
            
            if( isset( $_POST["mgid"] ) ) {
                $mg_result = $this->Menu->get_mg(array('mgid'=> $this->input->post("mgid",true)));
                //print_object($mg_result);exit;
                if(count($mg_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Group";
                } else {
                    $data['mgid'] = $mg_result[0]['mgid'];
                    $data['mglabel'] = $mg_result[0]['mglabel'];
                    $data['mgposition'] = $mg_result[0]['mgposition'];
                    $data['mgurl'] = $mg_result[0]['mgurl'];
                }
            }
            
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $data['content'] = $this->load->view('editmenugroup',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function deletestafftype() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST);exit;
            $data['ptitle'] = "Delete Menu Groups";
            if( isset( $_POST["mgid"] ) ) {
                $mg_result = $this->Menu->delete_mg(array('mgid'=> $this->input->post("mgid",true)));
                //print_object($mg_result);exit;
                if(!$mg_result) {
                    $data["errors"] = array('Could not find the selected Menu Group.');
                } else {
                    $data["successes"] = array('Menu group deleted successfully.');
                }
            }
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $data['content'] = $this->load->view('viewmenugroup',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function viewstafftype() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            
            $data['ptitle'] = "View Staff Type";
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $this->load->view('viewstafftype',$data);
       }
       
       function viewstafftypesingle() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            
            if( isset( $_POST["mgid"] ) ) {
                $data['ptitle'] = "View Menu Groups";
                $mg_result = $this->Menu->get_mg(array('mgid'=> $this->input->post("mgid",true)));
                //print_object($mg_result);exit;
                if(count($mg_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Group";
                } else {
                    $data['mgid'] = $mg_result[0]['mgid'];
                    $data['mglabel'] = $mg_result[0]['mglabel'];
                    $data['mgposition'] = $mg_result[0]['mgposition'];
                    $data['mgurl'] = $mg_result[0]['mgurl'];
                }
            } else {
                $data['ptitle'] = "View Staff Type";
                $navResult = $this->Dataset_db->buildNav();
            }
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $this->load->view('viewmenugroup',$data);
       }        
        
        function uploadstaffimage() {
            echo "test";
        }
       
       
        function addstaff() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddStaff']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"staff_name",
                        "staff_type_id",
                        "staff_gender",
                        "staff_marital_status",
                        "staff_dob",
                        "staff_email",
                        "staff_qualification",
                        "staff_mobile",
                        "staff_telephone",
                        "staff_address",
                        "staff_joining_date",
                        "staff_leaving_date",
                        "staff_rp_no",
                        "staff_rp_expiry",
                        "staff_nationality",
                        "staff_spouse_name",
                        "staff_spouse_mobile",
                        "staff_status"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('staff_name', 'Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('staff_type_id', 'Staff Type', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_gender', 'Gender', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_marital_status', 'Marital Status', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_dob', 'Date of birth', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_email', 'Email', 'trim|required|valid_email|min_length[3]|max_length[60]|xss_clean');
                $this->form_validation->set_rules('staff_mobile', 'Mobile', 'trim|required|min_length[3]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('staff_telephone', 'Telephone', 'trim|min_length[3]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('staff_address', 'Address', 'trim|required|min_length[3]|max_length[150]|xss_clean');
                $this->form_validation->set_rules('staff_nationality', 'Nationality', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_qualification', 'Qualification', 'trim|max_length[200]|xss_clean');
                $this->form_validation->set_rules('staff_spouse_name', 'Spouse Name', 'trim|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('staff_spouse_mobile', 'Spouse\'s Mobile', 'trim|min_length[3]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('staff_status', 'Employment Status', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_joining_date', 'Joining Date', 'trim|required|xss_clean');
                $this->form_validation->set_rules('staff_leaving_date', 'Releaved On', 'trim|xss_clean');
                $this->form_validation->set_rules('staff_rp_no', 'RP No.', 'trim|required|min_length[10]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('staff_rp_expiry', 'RP Expiry', 'trim|required|xss_clean');
                
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    //print_object($pdata); exit;
                    
                    $save_result = $this->Staff->save_staff($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Staff added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['stafftypelist'] = $this->Staff->get_stafftypes();
            $data['maritalstatuslist'] = $this->Dataset_db->get_maritalstatus();
            $data['countrylist'] = $this->Dataset_db->get_countries();
            $this->load->view('addstaff',$data);
        }
        
        function editmenuitem() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST); exit;
            $data['ptitle'] = "Edit Menu Item";
            $data['roles'] = $this->Menu->get_user_roles();
            if( isset( $_POST["btnUpdateMenuItem"] ) ) {
                $this->form_validation->set_rules('mgid', 'Menu Group', 'trim|required|xss_clean');
                $this->form_validation->set_rules('milabel', 'Menu Item Label', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('miurl', 'Url', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('miposition', 'Position', 'trim|required|numeric|xss_clean');
                
                $pdata = array();
                $fields = array(
						 "mgid"
						,"milabel"
						,"miurl"
                        ,"miposition"
                        ,"miactive"
                        ,"mihidden"				
						);
                foreach ($fields as $field) {
    				if (isset($_POST[$field])) {
    				    $pdata[$field] = $this->input->post($field,true);
    				} else { $data[$field] = ""; }   
    			}
                
                $data["postdata"] = $pdata;
                $proles = $this->input->post('miroles',true); 
                $data['postdata']['miroles'] = $proles;
                if ($this->form_validation->run() == FALSE) {
                    $data["errors"] = $this->form_validation->error_array();
                } else {
                    //print_object($pdata); exit;
                    $save_result = $this->Menu->save_mi($pdata,$this->input->post('miid',true));
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Menu item updated successfully.');
                        $miid = $this->input->post('miid',true);
                        $access_data = array();
                        if(count($proles) >0){
                            foreach ($data['roles'] as $role) {
                                if(in_array($role['roleid'], $proles)) {
                                    $in_data = array('roleid' => $role['roleid'], 'miid' => $miid, 'allowed' => 1);
                                } else {
                                    $in_data = array('roleid' => $role['roleid'], 'miid' => $miid, 'allowed' => 0);
                                }
                                array_push($access_data,$in_data);
                            }
                            $this->Menu->save_mi_access($access_data,$miid);    
                        }
                        $data['postdata'] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
           } 
           
           if( isset( $_POST["miid"] ) ) {
                $mi_result = $this->Menu->get_mi(array('miid'=> $this->input->post("miid",true)));
                //print_object($mi_result);exit;
                if(count($mi_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Item";
                } else {
                    $data['miid'] = $mi_result[0]['miid'];
                    $data['mgid'] = $mi_result[0]['mgid'];
                    $data['mglabel'] = $mi_result[0]['mglabel'];
                    $data['milabel'] = $mi_result[0]['milabel'];
                    $data['miposition'] = $mi_result[0]['miposition'];
                    $data['miurl'] = $mi_result[0]['miurl'];
                    $data['miactive'] = $mi_result[0]['miactive'];
                    $data['mihidden'] = $mi_result[0]['mihidden'];
                    $miroles = $this->Menu->get_mi_roles(array('miid'=> $mi_result[0]['miid']));
                    $data['miroles'] = $miroles;
                }
            } else {
                $data["errors"] = array("No or invalid menu item.");        
            }
            
            //print_object($data); exit;
            $navResult = $this->Dataset_db->buildNav();
            
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            
            $data['menugroups'] = $this->Menu->get_menu_groups();
            
            $data['content'] = $this->load->view('editmenuitem',$data);
            //print_object($data['menugroups']); exit;
            //$this->load->vars($data);
            //$this->load->view('admin/default_view'); 
       }
       
       function deletemenuitem() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST);exit;
            $data['ptitle'] = "Delete Menu Item";
            if( isset( $_POST["miid"] ) ) {
                $mg_result = $this->Menu->delete_mi(array('miid'=> $this->input->post("miid",true)));
                //print_object($mg_result);exit;
                if(!$mg_result) {
                    $data["errors"] = array('Could not find the selected Menu Item.');
                } else {
                    $data["successes"] = array('Menu item deleted successfully.');
                }
            }
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $data['content'] = $this->load->view('deleterow',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }

}       
/* End of file */